package com.njcky.utils.common;

import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Coordinate;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

import java.text.DecimalFormat;
import java.util.Arrays;

public class CoordinateTransformation {
    public static void main(String[] args) {
        //X=120.2176051 Y=31.8543421 Z=5.122
//        System.out.println(getCoordinate4549(120.2176051,31.8543421));
//        System.out.println(getCoordinate(3525721.7199534,520594.6516378));
    }
    //坐标转换4326转4549
    public static Double[] getCoordinate4549(Double x, Double y) {
        Double[] res = new Double[2];
        Coordinate tar = null;
        try {
            //封装点，这个是通用的，也可以用POINT（y,x）
            // private static WKTReader reader = new WKTReader( geometryFactory );
            Coordinate sour = new Coordinate(y, x);
            //这里要选择转换的坐标系是可以随意更换的
//            CoordinateReferenceSystem source = CRS.decode("EPSG:4326");
//            CoordinateReferenceSystem target = CRS.decode("EPSG:4549");
            CoordinateReferenceSystem source = CRS.decode("EPSG:4326");
            CoordinateReferenceSystem target = CRS.decode("EPSG:4549");
            //建立转换，下面两个我屏掉的转换方式会报出需要3/7参数的异常
            // MathTransform mathTransform = CRS.findMathTransform(source, target);
            //MathTransform mathTransform1 = CRS.findMathTransform(source, target, false);
            MathTransform transform = CRS.findMathTransform(source, target, true);
            tar = new Coordinate();
            //转换
            JTS.transform(sour, tar, transform);
        } catch (FactoryException | org.opengis.referencing.operation.TransformException e) {
            e.printStackTrace();
        }
        String[] split = (tar.toString().substring(1, tar.toString().length() - 1)).split(",");
        //经纬度精度
        DecimalFormat fm = new DecimalFormat("0.0000000");
        res[0] = Double.valueOf(fm.format(Double.valueOf(split[0])));
        res[1] = Double.valueOf(fm.format(Double.valueOf(split[1])));
//        System.out.println(Arrays.toString(res));
        return res;
    }


    //坐标转换4549转4326
    public static Double[] getCoordinate(Double x, Double y) {
        Double[] res = new Double[2];
        Coordinate tar = null;
        try {
            //封装点，这个是通用的，也可以用POINT（y,x）
            // private static WKTReader reader = new WKTReader( geometryFactory );
            Coordinate sour = new Coordinate(y, x);
            //这里要选择转换的坐标系是可以随意更换的
//            CoordinateReferenceSystem source = CRS.decode("EPSG:4326");
//            CoordinateReferenceSystem target = CRS.decode("EPSG:4549");
            CoordinateReferenceSystem source = CRS.decode("EPSG:4549");
            CoordinateReferenceSystem target = CRS.decode("EPSG:4326");
            //建立转换，下面两个我屏掉的转换方式会报出需要3/7参数的异常
            // MathTransform mathTransform = CRS.findMathTransform(source, target);
            //MathTransform mathTransform1 = CRS.findMathTransform(source, target, false);
            MathTransform transform = CRS.findMathTransform(source, target, true);
            tar = new Coordinate();
            //转换
            JTS.transform(sour, tar, transform);
        } catch (FactoryException | org.opengis.referencing.operation.TransformException e) {
            e.printStackTrace();
        }
        String[] split = (tar.toString().substring(1, tar.toString().length() - 1)).split(",");
        //经纬度精度
        DecimalFormat fm = new DecimalFormat("0.0000000");
        res[0] = Double.valueOf(fm.format(Double.valueOf(split[0])));
        res[1] = Double.valueOf(fm.format(Double.valueOf(split[1])));
//        System.out.println(Arrays.toString(res));
        return res;
    }
}
